<?php
// $Id$
/**
 * @file
 * Responsible for functions related to the forms of the website.
 */

/**
 * Function that build the 'Block Form'.
 */
function _patkauffmanwill_contact_form(&$form_state = NULL) {
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => 'Your Name',
    '#size' => '30',
    '#required' => TRUE,
  );
  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => 'Your Email',
    '#size' => '30',
    '#required' => TRUE,
  );
  $form['phone'] = array(
    '#type' => 'textfield',
    '#title' => 'Your Phone',
    '#size' => '30',
    '#required' => TRUE,
  );
  $form['country'] = array(
    '#type' => 'select',
    '#title' => 'Country',
    '#options' => _create_countries_array(),
    '#required' => TRUE
  );
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => 'Message',
    '#size' => '30',
    '#required' => TRUE,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Send',
  );

  return $form;
}

function _patkauffmanwill_contact_form_validate($form, &$form_state) {
  if (!valid_email_address($form_state['values']['email'])) {
    form_set_error('email', 'You must enter a valid email address');
  } elseif (!_validate_email_address($form_state['values']['email'])) {
    form_set_error('email', 'You must enter a valid email address');
  }
  
  if (!_validate_phone($form_state['values']['phone'])) {
    form_set_error('phone', 'Please enter a valid phone');
  }
  
  if ($form_state['values']['country'] == '-----------------------') {
    form_set_error('country', 'Please select a valid country');
  }
  
}

function _patkauffmanwill_contact_form_submit($form, &$form_state) {
  $node = node_load('7');
  
  $placeholder_text  = array('@name@', '@message@');
  $placeholder_value = array($node->field_recipient[0]['value'], $form_state['values']['message']);
  
  $params = array(
    '#body'    => str_replace($placeholder_text, $placeholder_value, $node->body),
    '#subject' => str_replace('@sender@', $form_state['values']['name'], $node->field_email_subject[0]['value']),
    '#from'    => $form_state['values']['email'],
  );
  
  drupal_mail('patkauffmanwill', 'contact', $node->field_email_recipient[0]['value'], language_default(), $params);
  
  $result = db_query('INSERT INTO {contact_form_email} VALUES (NULL, "' .$form_state['values']['name']. '", "' .$form_state['values']['email']. '", "' .$form_state['values']['phone']. '", "' .$form_state['values']['country']. '", "' .$form_state['values']['message']. '", ' .time(). ')');
  
  drupal_goto('thank-you');
}

function patkauffmanwill_mail ($key, &$message, $params) {
  switch ($key) {
    case 'contact':
      $message['subject'] = $params['#subject'];
      $message['body']    = $params['#body'];
      $message['from']    = $params['#from'];
      break;
  }
}

function _create_countries_array() {
  $country_list = array(
    "United States",
    "Canada",
    "United Kingdom",
    "-----------------------",
    "Afghanistan",
    "Albania",
    "Algeria",
    "Andorra",
    "Angola",
    "Antigua and Barbuda",
    "Argentina",
    "Armenia",
    "Australia",
    "Austria",
    "Azerbaijan",
    "Bahamas",
    "Bahrain",
    "Bangladesh",
    "Barbados",
    "Belarus",
    "Belgium",
    "Belize",
    "Benin",
    "Bhutan",
    "Bolivia",
    "Bosnia and Herzegovina",
    "Botswana",
    "Brazil",
    "Brunei",
    "Bulgaria",
    "Burkina Faso",
    "Burundi",
    "Cambodia",
    "Cameroon",
    "Cape Verde",
    "Central African Republic",
    "Chad",
    "Chile",
    "China",
    "Colombi",
    "Comoros",
    "Congo (Brazzaville)",
    "Congo",
    "Costa Rica",
    "Cote d'Ivoire",
    "Croatia",
    "Cuba",
    "Cyprus",
    "Czech Republic",
    "Denmark",
    "Djibouti",
    "Dominica",
    "Dominican Republic",
    "East Timor (Timor Timur)",
    "Ecuador",
    "Egypt",
    "El Salvador",
    "Equatorial Guinea",
    "Eritrea",
    "Estonia",
    "Ethiopia",
    "Fiji",
    "Finland",
    "France",
    "Gabon",
    "Gambia, The",
    "Georgia",
    "Germany",
    "Ghana",
    "Greece",
    "Grenada",
    "Guatemala",
    "Guinea",
    "Guinea-Bissau",
    "Guyana",
    "Haiti",
    "Honduras",
    "Hungary",
    "Iceland",
    "India",
    "Indonesia",
    "Iran",
    "Iraq",
    "Ireland",
    "Israel",
    "Italy",
    "Jamaica",
    "Japan",
    "Jordan",
    "Kazakhstan",
    "Kenya",
    "Kiribati",
    "Korea, North",
    "Korea, South",
    "Kuwait",
    "Kyrgyzstan",
    "Laos",
    "Latvia",
    "Lebanon",
    "Lesotho",
    "Liberia",
    "Libya",
    "Liechtenstein",
    "Lithuania",
    "Luxembourg",
    "Macedonia",
    "Madagascar",
    "Malawi",
    "Malaysia",
    "Maldives",
    "Mali",
    "Malta",
    "Marshall Islands",
    "Mauritania",
    "Mauritius",
    "Mexico",
    "Micronesia",
    "Moldova",
    "Monaco",
    "Mongolia",
    "Morocco",
    "Mozambique",
    "Myanmar",
    "Namibia",
    "Nauru",
    "Nepa",
    "Netherlands",
    "New Zealand",
    "Nicaragua",
    "Niger",
    "Nigeria",
    "Norway",
    "Oman",
    "Pakistan",
    "Palau",
    "Panama",
    "Papua New Guinea",
    "Paraguay",
    "Peru",
    "Philippines",
    "Poland",
    "Portugal",
    "Qatar",
    "Romania",
    "Russia",
    "Rwanda",
    "Saint Kitts and Nevis",
    "Saint Lucia",
    "Saint Vincent",
    "Samoa",
    "San Marino",
    "Sao Tome and Principe",
    "Saudi Arabia",
    "Senegal",
    "Serbia and Montenegro",
    "Seychelles",
    "Sierra Leone",
    "Singapore",
    "Slovakia",
    "Slovenia",
    "Solomon Islands",
    "Somalia",
    "South Africa",
    "Spain",
    "Sri Lanka",
    "Sudan",
    "Suriname",
    "Swaziland",
    "Sweden",
    "Switzerland",
    "Syria",
    "Taiwan",
    "Tajikistan",
    "Tanzania",
    "Thailand",
    "Togo",
    "Tonga",
    "Trinidad and Tobago",
    "Tunisia",
    "Turkey",
    "Turkmenistan",
    "Tuvalu",
    "Uganda",
    "Ukraine",
    "United Arab Emirates",
    "Uruguay",
    "Uzbekistan",
    "Vanuatu",
    "Vatican City",
    "Venezuela",
    "Vietnam",
    "Yemen",
    "Zambia",
    "Zimbabwe"
  );
  
  foreach ($country_list as $key => $value) {
    
    $value_name = str_replace(' ', '-', $value);
    
    $country_list_values[strtolower($value_name)] = $value;
  }
  
  return $country_list_values;
  
}

function _validate_email_address ($email) {
  if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
    return TRUE;
  } else {
    return FALSE;
  }
}

function _validate_phone ($phone) {
  $phone = str_replace(array('+', ' '), array('', ''), $phone);
  
  if (!is_numeric($phone)) {
    return FALSE;
  }
  
  return TRUE;
  
}